/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * Login.java
 *
 * Created on Aug 24, 2011, 7:42:20 AM
 */
package cw.ui;

import com.jtattoo.plaf.aero.AeroLookAndFeel;
import cw.biz.UserBiz;
import cw.controller.UserJpaController;
import cw.ulti.CryptoLibrary;
import cw.ulti.Validator;
import java.awt.Color;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.JDialog;
import javax.swing.JFrame;
import javax.swing.UIManager;

/**
 *
 * @author VinhTC
 */
public class Login extends javax.swing.JFrame {

    UserBiz ubiz = new UserBiz();
    CryptoLibrary crypto = new CryptoLibrary();
    private static String us = "";

    /** Creates new form Login */
    public Login() {
        initComponents();

    }

    private Login(Login login) {
        throw new UnsupportedOperationException("Not yet implemented");
    }

//    private Login(Login login) {
//        throw new UnsupportedOperationException("Not yet implemented");
//    }
    public String getUs() {
        return us;
    }

    public void setUs(String us) {
        this.us = us;
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jPanel1 = new javax.swing.JPanel();
        jLabel1 = new javax.swing.JLabel();
        jLabel2 = new javax.swing.JLabel();
        jtxtUserName = new javax.swing.JTextField();
        jtxtPassword = new javax.swing.JPasswordField();
        jbtnLogin = new javax.swing.JButton();
        jLabel5 = new javax.swing.JLabel();
        jLabel6 = new javax.swing.JLabel();
        jlblLoginStatus = new javax.swing.JLabel();
        jLabel3 = new javax.swing.JLabel();
        jLabel4 = new javax.swing.JLabel();
        jbtnConfigDatabase = new javax.swing.JButton();
        jbtnForgotPass = new javax.swing.JButton();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
        setTitle("CrossWords - Admin login system");
        setAlwaysOnTop(true);
        setCursor(new java.awt.Cursor(java.awt.Cursor.DEFAULT_CURSOR));
        setName("frmLogin"); // NOI18N

        jPanel1.setBorder(javax.swing.BorderFactory.createTitledBorder("Fill username and password"));

        jLabel1.setFont(new java.awt.Font("Tahoma", 1, 12));
        jLabel1.setText("UserName: ");

        jLabel2.setFont(new java.awt.Font("Tahoma", 1, 12));
        jLabel2.setText("Password: ");

        jtxtUserName.setToolTipText("Please enter your user name");
        jtxtUserName.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jtxtUserNameActionPerformed(evt);
            }
        });

        jtxtPassword.setToolTipText("Please enter your password");

        jbtnLogin.setText("Login >");
        jbtnLogin.setToolTipText("Click here to login to admin system");
        jbtnLogin.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jbtnLoginActionPerformed(evt);
            }
        });

        jLabel5.setForeground(new java.awt.Color(255, 0, 51));
        jLabel5.setText("(*)");

        jLabel6.setForeground(new java.awt.Color(255, 0, 51));
        jLabel6.setText("(*)");

        jLabel3.setForeground(new java.awt.Color(153, 153, 153));
        jLabel3.setText("(*) is requiremented");

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addComponent(jlblLoginStatus, javax.swing.GroupLayout.DEFAULT_SIZE, 337, Short.MAX_VALUE)
                        .addGap(15, 15, 15))
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel1Layout.createSequentialGroup()
                        .addComponent(jbtnLogin)
                        .addGap(134, 134, 134))
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false)
                            .addComponent(jLabel3, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                            .addGroup(javax.swing.GroupLayout.Alignment.LEADING, jPanel1Layout.createSequentialGroup()
                                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                                    .addGroup(jPanel1Layout.createSequentialGroup()
                                        .addComponent(jLabel1)
                                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                        .addComponent(jtxtUserName, javax.swing.GroupLayout.PREFERRED_SIZE, 248, javax.swing.GroupLayout.PREFERRED_SIZE))
                                    .addGroup(jPanel1Layout.createSequentialGroup()
                                        .addComponent(jLabel2)
                                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                        .addComponent(jtxtPassword)))
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                    .addComponent(jLabel6)
                                    .addComponent(jLabel5))))
                        .addContainerGap(15, Short.MAX_VALUE))))
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel1)
                    .addComponent(jtxtUserName, javax.swing.GroupLayout.PREFERRED_SIZE, 25, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel5, javax.swing.GroupLayout.PREFERRED_SIZE, 21, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(26, 26, 26)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel2)
                    .addComponent(jtxtPassword, javax.swing.GroupLayout.PREFERRED_SIZE, 25, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel6, javax.swing.GroupLayout.PREFERRED_SIZE, 21, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(jLabel3)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addComponent(jbtnLogin)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jlblLoginStatus, javax.swing.GroupLayout.PREFERRED_SIZE, 21, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap())
        );

        jLabel4.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
        jLabel4.setIcon(new javax.swing.ImageIcon(getClass().getResource("/image/PuzzleLogo.png"))); // NOI18N
        jLabel4.setToolTipText("Welcome to crossword game");

        jbtnConfigDatabase.setText("Config database");
        jbtnConfigDatabase.setToolTipText("Config database parameters");
        jbtnConfigDatabase.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jbtnConfigDatabaseActionPerformed(evt);
            }
        });

        jbtnForgotPass.setText("Forgot Password ?");
        jbtnForgotPass.setToolTipText("Are you forgot your password???");
        jbtnForgotPass.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jbtnForgotPassActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGap(61, 61, 61)
                .addComponent(jbtnConfigDatabase)
                .addGap(45, 45, 45)
                .addComponent(jbtnForgotPass)
                .addContainerGap(56, Short.MAX_VALUE))
            .addComponent(jLabel4, javax.swing.GroupLayout.PREFERRED_SIZE, 394, Short.MAX_VALUE)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addComponent(jLabel4, javax.swing.GroupLayout.PREFERRED_SIZE, 87, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addGap(18, 18, 18)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jbtnConfigDatabase)
                    .addComponent(jbtnForgotPass))
                .addContainerGap())
        );

        pack();
        java.awt.Dimension screenSize = java.awt.Toolkit.getDefaultToolkit().getScreenSize();
        java.awt.Dimension dialogSize = getSize();
        setLocation((screenSize.width-dialogSize.width)/2,(screenSize.height-dialogSize.height)/2);
    }// </editor-fold>//GEN-END:initComponents

    private void jtxtUserNameActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jtxtUserNameActionPerformed
        // TODO add your handling code here:
    }//GEN-LAST:event_jtxtUserNameActionPerformed

    private void jbtnLoginActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jbtnLoginActionPerformed
        String username = jtxtUserName.getText().trim();
        jlblLoginStatus.setText("");
        jlblLoginStatus.setForeground(Color.red);
        String pass = String.valueOf(jtxtPassword.getPassword()).trim();
        if (!Validator.checkNull(username)) {
            jtxtUserName.setFocusable(true);
            jlblLoginStatus.setText("Please enter user name!");
            jlblLoginStatus.setForeground(Color.red);
        } else if (!Validator.checkNull(pass)) {
            jtxtPassword.setFocusable(true);
            jlblLoginStatus.setText("Please enter password!");
            jlblLoginStatus.setForeground(Color.red);
        } else if (!Validator.checkNull(username) && !Validator.checkNull(pass)) {
            jlblLoginStatus.setText("Please enter username and password!");
            jlblLoginStatus.setForeground(Color.red);
        } else {
            try {
                if (ubiz.login(crypto.encrypt(username), crypto.encrypt(pass))) {
                    AdminMainPanel amp = new AdminMainPanel(username);
                    amp.setVisible(true);
                    this.dispose();
                } else {
                    jlblLoginStatus.setText("Login error...!");
                    jlblLoginStatus.setForeground(Color.red);
                    jbtnForgotPass.setToolTipText("May be you forgot password???");
                }
            } catch (Exception e) {
                Logger.getLogger(Login.class.getName()).log(Level.SEVERE, null, e);
                jlblLoginStatus.setText("Login error...! Maybe cannot connection to database");
                jlblLoginStatus.setForeground(Color.red);
            }
        }
    }//GEN-LAST:event_jbtnLoginActionPerformed

    private void jbtnConfigDatabaseActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jbtnConfigDatabaseActionPerformed
        JDialog frame = new JDialog();
        frame.setContentPane(new DatabaseConfig());
        frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
        frame.setResizable(false);
        frame.setAlwaysOnTop(true);
        frame.setModal(true);
        frame.setTitle("Cross Words - Config database");
        frame.setLocationByPlatform(true);
        frame.pack();
        frame.setVisible(true);
    }//GEN-LAST:event_jbtnConfigDatabaseActionPerformed

    private void jbtnForgotPassActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jbtnForgotPassActionPerformed
        JDialog frame = new JDialog();
        frame.setContentPane(new ForgotPass());
        frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
        frame.setResizable(false);
        frame.setAlwaysOnTop(true);
        frame.setModal(true);
        frame.setTitle("Cross Words - Recover password system");
        frame.setLocationByPlatform(true);
        frame.pack();
        frame.setVisible(true);

    }//GEN-LAST:event_jbtnForgotPassActionPerformed

    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        java.awt.EventQueue.invokeLater(new Runnable() {

            public void run() {
                try {
//                    UIManager.setLookAndFeel(new McWinLookAndFeel());
//                    UIManager.setLookAndFeel(new LunaLookAndFeel());
//                    UIManager.setLookAndFeel(new AcrylLookAndFeel());
                    UIManager.setLookAndFeel(new AeroLookAndFeel());
//                    UIManager.setLookAndFeel(new AluminiumLookAndFeel());
                    
                    
                } catch (Exception e) {
                    e.printStackTrace();
                }
                Login login = new Login();
                login.setResizable(false);
                login.setVisible(true);

            }
        });
    }
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JLabel jLabel5;
    private javax.swing.JLabel jLabel6;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JButton jbtnConfigDatabase;
    private javax.swing.JButton jbtnForgotPass;
    private javax.swing.JButton jbtnLogin;
    private javax.swing.JLabel jlblLoginStatus;
    private javax.swing.JPasswordField jtxtPassword;
    private javax.swing.JTextField jtxtUserName;
    // End of variables declaration//GEN-END:variables
}
